//
//  LeftTableViewController.m
//  MemoryTravel
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 fcx. All rights reserved.
//

#import "LeftTableViewController.h"

@interface LeftTableViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *leftTableView;
@property (nonatomic, strong) NSMutableArray *leftArray;

@property (nonatomic, assign) BOOL isUpLoading;
@property (nonatomic, assign) NSInteger page;


@property (nonatomic, strong) UIView *orderView;
@property (nonatomic, strong) UIScrollView *littleScrollView;
@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic, strong) UIImageView *orderImageView;
@property (nonatomic, retain) UIPageControl *pageC;
@property (nonatomic, retain) UIView *orderBottomView;
@property (nonatomic, retain) UILabel *orderLabel;


@end

@implementation LeftTableViewController

- (void)viewWillAppear:(BOOL)animated{

    [[self navigationController] setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad {

    [super viewDidLoad];

    self.leftArray = [NSMutableArray array];//初始化数组

    self.leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Width, Height - 64 - 49) style:UITableViewStylePlain];
    self.leftTableView.delegate = self;
    self.leftTableView.dataSource = self;
    self.leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.leftTableView.showsVerticalScrollIndicator = NO;//取消滑块
    self.leftTableView.scrollsToTop = YES;//快速回到顶部
    [self.leftTableView registerClass:[LeftTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.leftTableView];

#pragma mark - 轮播图
    self.orderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 180 * HEI)];
    self.leftTableView.tableHeaderView = self.orderView;
    self.littleScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, self.orderView.frame.size.height)];
    [self.orderView addSubview:self.littleScrollView];

    self.imageArray = [NSMutableArray arrayWithObjects:@"15.jpg",
                       @"10.jpg",
                       @"12.jpg",
                       @"13.jpg",
                       @"14.jpg",
                       @"15.jpg",
                       @"10.jpg",nil];
    self.littleScrollView.contentSize = CGSizeMake(WIDTH * self.imageArray.count, 0);
    for (int i = 0; i < self.imageArray.count; i++) {
        self.orderImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, self.orderView.frame.size.height)];
        NSString *name = [self.imageArray objectAtIndex:i];
        self.orderImageView.image = [UIImage imageNamed:name];
        [self.littleScrollView addSubview:self.orderImageView];
        self.orderImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [self.orderImageView addGestureRecognizer:tap];

    }
    self.littleScrollView.pagingEnabled = YES;
    self.littleScrollView.bounces = NO;
    self.littleScrollView.showsHorizontalScrollIndicator = NO;
    self.littleScrollView.contentOffset = CGPointMake(WIDTH, 0);
    self.littleScrollView.delegate = self;
#pragma mark 小点点
    self.orderBottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 150 * HEI, WIDTH, 30 * HEI)];
    self.orderBottomView.backgroundColor = [UIColor colorWithWhite:0.247 alpha:0.5];
    [self.orderView addSubview:self.orderBottomView];

    self.orderLabel = [[UILabel alloc] initWithFrame:CGRectMake(30 * WID, 0, 220 * WID, self.orderBottomView.frame.size.height)];
    self.orderLabel.text = @"星海广场";
    self.orderLabel.textColor = [UIColor whiteColor];
    [self.orderBottomView addSubview:self.orderLabel];

    self.pageC = [[UIPageControl alloc] initWithFrame:CGRectMake(self.orderLabel.frame.size.width + 50 * WID, 0, 100 * WID, self.orderBottomView.frame.size.height)];
    self.pageC.backgroundColor = [UIColor clearColor];
    [self.orderBottomView addSubview:self.pageC];
    self.pageC.numberOfPages = self.imageArray.count - 2;
    self.pageC.currentPageIndicatorTintColor = [UIColor whiteColor];
    self.pageC.pageIndicatorTintColor = [UIColor lightGrayColor];
    self.pageC.currentPage = 0;
    [self.pageC addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];

#pragma mark - 定时器
    [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(action) userInfo:@"" repeats:YES];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeToNight:) name:@"night" object:nil];

    [self addHeaderRefresh];

    [self addFooterRefresh];
}

#pragma mark -- 刷新
- (void)addHeaderRefresh{

    self.leftTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

        self.isUpLoading = NO;

        self.page = 1;

        [self getAFNetWorkingData];
    }];

    [self.leftTableView.header beginRefreshing];
}

#pragma mark -- 加载
- (void)addFooterRefresh{

    self.leftTableView.footer  = [MJRefreshAutoFooter footerWithRefreshingBlock:^{

        self.isUpLoading = YES;

        self.page += 1;

        [self getAFNetWorkingData];
    }];
}

#pragma mark -- 网络请求
- (void)getAFNetWorkingData{

    NSString *url = [NSString stringWithFormat: @"http://chanyouji.com/api/trips/featured.json?page=%ld", self.page];

    [AppTools getDataWithUrlStr:url andBlock:^(id result) {

        if (!self.isUpLoading) {

            [self.leftArray removeAllObjects];
        }

        for (NSMutableDictionary *dic in result) {

            TravelsModel *model = [TravelsModel modelWithDictionary:dic];

            [self.leftArray addObject :model];
        }

        [self.leftTableView.header endRefreshing];
        [self.leftTableView.footer endRefreshing];
        [self.leftTableView reloadData];//刷新
    }];
}

#pragma mark -- 行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.leftArray.count;
}

#pragma mark -- cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *travelscell = @"cell";

    LeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:travelscell];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    TravelsModel *travelsMdoel = [self.leftArray objectAtIndex:indexPath.row];
    
    cell.travelsModel = travelsMdoel;

    return cell;
}

#pragma mark -- 行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPat{

    return 200;
}

#pragma mark -- 触摸
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];//点中置灰  放开取消置灰

    LeftDetailTableViewController *leftDetail = [LeftDetailTableViewController new];

    TravelsModel *leftmodel  = [self.leftArray objectAtIndex:indexPath.row];

    leftDetail.pageID = leftmodel.pID;//传入二级页面id

    leftDetail.topImages = leftmodel.front_cover_photo_url;

    leftDetail.namelabel = leftmodel.name;

    NSString *time;

    if (leftmodel.start_date == nil || [leftmodel.start_date isEqualToString:@"(null)"]) {

        time = [NSString stringWithFormat:@"%@天/%@图",leftmodel.days,leftmodel.photos_count];

    }else{

        time = [NSString stringWithFormat:@"%@/%@天/%@图",leftmodel.start_date, leftmodel.days, leftmodel.photos_count];
    }

    leftDetail.titlelabel = time;

    leftDetail.personImage = leftmodel.anotherUser.image;

    leftDetail.hidesBottomBarWhenPushed = YES;//隐藏tabbar

    [self.navigationController pushViewController:leftDetail animated:YES];
}

-(void)action{
    [UIView animateWithDuration:0.5 animations:^{

        [self.littleScrollView setContentOffset:CGPointMake(self.littleScrollView.contentOffset.x + WIDTH, 0) animated:YES];
    }];
    if (self.littleScrollView.contentOffset.x == WIDTH * 5) {
        self.littleScrollView.contentOffset = CGPointMake(0, 0);
    }
    self.pageC.currentPage = self.littleScrollView.contentOffset.x / WIDTH;

    if (self.pageC.currentPage == 0) {
        self.orderLabel.text = @"星海公园";
    }else if (self.pageC.currentPage == 1){
        self.orderLabel.text = @"海之韵公园";
    }else if (self.pageC.currentPage == 2){
        self.orderLabel.text = @"中山广场";
    }else if (self.pageC.currentPage == 3){
        self.orderLabel.text = @"人民广场";
    }else{
        self.orderLabel.text = @"奥林匹克广场";
    }
}

-(void)pageAction:(UIPageControl *)pageC{

    [self.littleScrollView setContentOffset:CGPointMake(WIDTH * (pageC.currentPage + 1), 0) animated:YES];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (self.littleScrollView.contentOffset.x == 0) {
        self.littleScrollView.contentOffset = CGPointMake(WIDTH * 5, 0);
    }else if(self.littleScrollView.contentOffset.x == WIDTH * 6){
        self.littleScrollView.contentOffset = CGPointMake(WIDTH, 0);
    }
    self.pageC.currentPage = self.littleScrollView.contentOffset.x / WIDTH - 1;
    if (self.pageC.currentPage == 0) {
        self.orderLabel.text = @"星海广场";
    }else if (self.pageC.currentPage == 1){
        self.orderLabel.text = @"海之韵广场";
    }else if (self.pageC.currentPage == 2){
        self.orderLabel.text = @"中山广场";
    }else if (self.pageC.currentPage == 3){
        self.orderLabel.text = @"人民广场";
    }else{
        self.orderLabel.text = @"奥林匹克广场";
    }
    
}

- (void)tapAction:(UITapGestureRecognizer *)tap{


    //******重要属性 tap.view
    LeftWebViewController *webVC = [[LeftWebViewController alloc] init];
    if (self.littleScrollView.contentOffset.x/WIDTH == 1 || self.littleScrollView.contentOffset.x / WIDTH == 6) {
        webVC.webUrl = @"http://www.mafengwo.cn/poi/9288.html";
    }else if (self.littleScrollView.contentOffset.x/WIDTH == 2 ){
        webVC.webUrl = @"http://www.mafengwo.cn/poi/9302.html";
    }else if (self.littleScrollView.contentOffset.x/WIDTH == 3){
        webVC.webUrl = @"http://www.mafengwo.cn/i/1264008.html";
    }else if (self.littleScrollView.contentOffset.x/WIDTH == 4){
        webVC.webUrl = @"http://www.mafengwo.cn/poi/5422656.html";
    }else{
        webVC.webUrl = @"http://http://www.mafengwo.cn/poi/9371.html";
    }

    [self.navigationController pushViewController:webVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation

 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
